/**
 * 字典服务文件
 */

const { getListByType, addDictionary, updateDictionary } = require("../db/dictionary");
const { DICTIONARY } = require('../utils/CONST');

/**
 * 获取科目列表
 * @param {*} ctx 
 */
const getSubjectList = async (ctx) => {
    try {
        const list = await getListByType(DICTIONARY.科目);
        ctx.body = {
            code: 1,
            data: list
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '科目列表获取失败'
        }
    }
}

/**
 * 获取职称列表
 * @param {*} ctx 
 */
const getPositionList = async (ctx) => {
    try {
        const list = await getListByType(DICTIONARY.职称);
        ctx.body = {
            code: 1,
            data: list
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '职称列表获取失败'
        }
    }
}

/**
 * 获取日志列表
 * @param {*} ctx 
 */
const getLogsList = async ctx => {
    try {
        const list = await getListByType(DICTIONARY.日志);
        ctx.body = {
            code: 1,
            data: list
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '日志列表获取失败'
        }
    }
}

/**
 * 根据类型获取字典列表
 * @param {*} ctx 
 */
const getList = async ctx => {
    try {
        const list = await getListByType(ctx.request.params.type);
        const keys = Object.keys(DICTIONARY);
        const types = {};
        for (const key of keys) {
            types[DICTIONARY[key]] = key;
        }
        for (const item of list) {
            item.typeStr = types[item.type]
        }
        ctx.body = {
            code: 1,
            data: list
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '字典获取失败'
        }
    }
}

/**
 * 获取类型
 * @param {*} ctx 
 */
const getTypes = async ctx => {
    try {
        ctx.body = {
            code: 1,
            data: Object.keys(DICTIONARY).map(key => ({ label: key, value: DICTIONARY[key] }))
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '字典类型获取失败'
        }
    }
}


/**
 * 添加字典
 * @param {*} ctx 
 */
const add = async ctx => {
    try {
        await addDictionary(ctx.request.body);
        ctx.body = {
            code: 1,
            msg: '字典添加成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '字典添加失败'
        }
    }
}


/**
 * 修改字典
 * @param {*} ctx 
 */
const update = async ctx => {
    try {
        await updateDictionary(ctx.request.body);
        ctx.body = {
            code: 1,
            msg: '字典修改成功'
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '字典修改失败'
        }
    }
}

/**
 * 获取年级列表
 * @param {*} ctx 
 */
const getGradeList = async ctx => {
    try {
        const data = await getListByType(DICTIONARY.年级);
        ctx.body = {
            code: 1,
            data
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '年级列表获取失败'
        }
    }
}

/**
 * 获取学生状态
 * @param {*} ctx 
 */
const getStudentStatus = async ctx => {
    try {
        const data = await getListByType(DICTIONARY.学生状态);
        ctx.body = {
            code: 1,
            data
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '学生状态列表获取失败'
        }
    }
}

/**
 * 获取学生档案
 * @param {*} ctx 
 */
const getStudentRecords = async ctx => {
    try {
        const data = await getListByType(DICTIONARY.学生档案);
        ctx.body = {
            code: 1,
            data
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '学生档案列表获取失败'
        }
    }
}

/**
 * 获取学生费用类型
 * @param {*} ctx 
 */
const getStudentFee = async ctx => {
    try {
        const data = await getListByType(DICTIONARY.学生费用);
        ctx.body = {
            code: 1,
            data
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '学生费用类型列表获取失败'
        }
    }
}

/**
 * 获取教师考勤类型
 * @param {*} ctx 
 */
const getAttendanceList = async ctx => {
    try {
        const data = await getListByType(DICTIONARY.老师考勤);
        ctx.body = {
            code: 1,
            data
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '教师考勤类型列表获取失败'
        }
    }
}

/**
 * 获取审批状态
 * @param {*} ctx 
 */
const getAuditStatus = async ctx => {
    try {
        const data = await getListByType(DICTIONARY.审批状态);
        ctx.body = {
            code: 1,
            data
        }
    } catch (error) {
        ctx.app.emit('error', error);
        ctx.body = {
            code: 0,
            msg: '审批状态列表获取失败'
        }
    }
}

module.exports = {
    add,
    update,
    getList,
    getTypes,
    getLogsList,
    getGradeList,
    getStudentFee,
    getSubjectList,
    getAuditStatus,
    getPositionList,
    getStudentStatus,
    getStudentRecords,
    getAttendanceList
}